/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By 深圳中科鑫智科技有限公司
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.app.service.userAuth.impl;

import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cereshop.app.dao.userAuth.UserRealnameAuthDAO;
import com.shop.cereshop.commons.domain.agreement.Agreement;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.user.faceCheck.FaceCheckModel;
import com.shop.cereshop.commons.domain.user.faceCheck.respond.FaceCheckRespond;
import com.shop.cereshop.commons.domain.userAuth.UserRealnameAuth;
import com.shop.cereshop.app.service.userAuth.UserRealnameAuthService;

import com.shop.cereshop.commons.domain.userAuth.UserRealnameAuthRespond;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 * 业务实现类
 * 用户实名认证表
 * </p>
 *
 * @author
 * @date 2025-08-07
 */
@Slf4j
@Service
public class UserRealnameAuthServiceImpl extends ServiceImpl<UserRealnameAuthDAO, UserRealnameAuth> implements UserRealnameAuthService {

    @Resource
    private UserRealnameAuthDAO userRealnameAuthDAO;

    @Override
    @Transactional
    public Boolean saveUserAuth(CereBuyerUser user, FaceCheckRespond faceCheckRespond, FaceCheckModel model) {
        UserRealnameAuth userRealnameAuth = UserRealnameAuth.builder()
                .userId(user.getBuyerUserId())
                .realName(faceCheckRespond.getName())
                .idCardType(1)
                .idCardNumber(faceCheckRespond.getIdCardNumber())
                .authStatus(faceCheckRespond.getSuccess() ? 1 : 3)
                .authTime(LocalDateTime.now())
                .idCardHandheld(StrUtil.isNotBlank(model.getImageBase64()) ? model.getImageBase64() : model.getImageUrl())
                .requestData(faceCheckRespond.getReqParam())
                .responseData(faceCheckRespond.getReqResp())
                .statusCode(faceCheckRespond.getSuccess() ? "200" : "500")
                .build();
        return saveOrUpdate(userRealnameAuth);
    }

    @Override
    public UserRealnameAuthRespond findAuthInfoByUserId(Long buyerUserId) {
        UserRealnameAuth userId = userRealnameAuthDAO.selectOne(new QueryWrapper<UserRealnameAuth>().eq("user_id", buyerUserId));
        if (userId == null) {
            return null;
        }
        return UserRealnameAuthRespond.builder()
                .userId(userId.getUserId())
                .realName(DesensitizedUtil.chineseName(userId.getRealName()))
                .idCardNumber(DesensitizedUtil.idCardNum(userId.getIdCardNumber(), 6, 4))
                .idCardType(userId.getIdCardType())
                .idCardHandheld(userId.getIdCardHandheld())
                .build();
    }

    @Override
    public Integer updateStatue(Integer statue, Long userId) {
        return userRealnameAuthDAO.updateStatue(statue,userId);
    }
}
